package com.haochang.chunkvod.app.utils;

import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;

/**
 * 屏幕锁屏唤醒工具类
 *@author qintao
 *created at 2016/12/12 14:27
 */

public class KeyguardManagerUtil
{
	private KeyguardManager.KeyguardLock kl;
	private PowerManager.WakeLock wl;
	private static volatile KeyguardManagerUtil singleton;

	private KeyguardManagerUtil()
	{
	}

	public static KeyguardManagerUtil getInstance()
	{
		if (singleton == null)
		{
			synchronized (KeyguardManagerUtil.class)
			{
				if (singleton == null)
				{
					singleton = new KeyguardManagerUtil();
				}
			}
		}
		return singleton;
	}

	public void keyguardAndAcquire(Context context)
	{
		if (context != null)
		{
			//方法1
			//			if (null == kl)
			//			{
			//				//得到键盘锁管理器对象
			//				KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
			//				kl = km.newKeyguardLock("unLock");
			//			}
			//			//解锁
			//			if (kl != null)
			//				kl.disableKeyguard();
			//
			//			if (null == wl)
			//			{
			//				PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
			//				//获取PowerManager.WakeLock对象，后面的参数|表示同时传入两个值，最后的是调试用的Tag
			//				wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
			//			}
			//			//点亮屏幕
			//
			//			if (wl != null)
			//				wl.acquire();
			//			wl.release();//如果这句话不调用屏幕不会高亮显示,所以暂时不用这方法

			//方法2
			KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
			//解锁
			if (km.inKeyguardRestrictedInputMode())
			{
				KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
				kl.disableKeyguard();
			}
			//获取电源管理器对象
			PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
			//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
			PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
			//点亮屏幕
			wl.acquire();
			//释放
			wl.release();
		}
	}

	/**
	 * 释放资源
	 */
	public void release()
	{
		if (kl != null)
			kl.reenableKeyguard();
		if (wl != null)
			wl.release();
	}
}
